package myApp.KnorrAndroid.model;

import java.util.Vector;

import myApp.Constant.Constants;
import myApp.KnorrAndroid.common.ActionEvent;
import myApp.KnorrAndroid.common.KnorrInfo;
import myApp.KnorrAndroid.common.ServerPath;
import myApp.KnorrAndroid.lib.network.http.HTTPListenner;
import myApp.KnorrAndroid.lib.network.http.HTTPMessage;
import myApp.KnorrAndroid.lib.network.http.HTTPMultiPartRequest;
import myApp.KnorrAndroid.lib.network.http.HTTPRequest;
import myApp.KnorrAndroid.lib.network.http.HTTPResponse;
import myApp.KnorrAndroid.lib.network.http.HttpAsyncTask;
import myApp.KnorrAndroid.lib.network.http.MultiPartInputStream;
import myApp.KnorrAndroid.lib.network.http.NetworkUtil;
import android.graphics.Bitmap;

public abstract class AbstractModelService implements HTTPListenner{

	public void onReceiveError(HTTPResponse response) {
		// TODO Auto-generated method stub
		
	}

	public void onReceiveData(HTTPMessage mes) {
		// TODO Auto-generated method stub
		
	}
	
	
	@SuppressWarnings("rawtypes")
	protected HTTPRequest sendHttpRequest(String method, Vector data, ActionEvent actionEvent) {
		StringBuffer strBuffer = new StringBuffer();
		strBuffer.append(NetworkUtil.getJSONString(method, data));
		HTTPRequest re = new HTTPRequest();
		re.setUrl(ServerPath.SERVER_PATH);		
		re.setAction(actionEvent.action);
		re.setContentType(HTTPMessage.CONTENT_JSON);
		re.setMethod(Constants.HTTPCONNECTION_POST);
		re.setDataText(strBuffer.toString());
		re.setObserver(this);
		re.setUserData(actionEvent);
		new HttpAsyncTask(re).execute();
		return re;
	}
	
	@SuppressWarnings("rawtypes")
	protected HTTPRequest httpMultiPartRequest(String method, Vector data, String imgPath, ActionEvent actionEvent,
			String fileName, String fileField, String fileType) {
		Bitmap bmp = KnorrInfo.getInstance().getBitmapData();
		HTTPRequest re = new HTTPMultiPartRequest();
		re.setContentType(HTTPMessage.MULTIPART_JSON);
		re.setDataTypeSend(HTTPRequest.CONTENT_TYPE_BINARY);
		MultiPartInputStream multiPartStream;
		try {
			if(bmp != null){			
					multiPartStream = new MultiPartInputStream(
					NetworkUtil.getJSONString(method,data), bmp, fileName,fileField,fileType);
				
			}else{
				multiPartStream = new MultiPartInputStream(
						NetworkUtil.getJSONString(method,
								data), imgPath, fileName,fileField,fileType);
			}
			((HTTPMultiPartRequest) re).setMultipartStream(multiPartStream);
			re.setUrl(ServerPath.SERVER_PATH);
			re.setAction(actionEvent.action);
			re.setMethod("POST");
			re.setObserver(this);
			re.setUserData(actionEvent);

			new HttpAsyncTask(re).execute();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return re;
	}
}

